package com.collect.zambia.zhiScene.zhiCheckIn;

import android.annotation.SuppressLint;
import android.view.View;
import android.view.ViewGroup;

import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.collect.zambia.R;
import com.collect.zambia.zhiAdater.zhiCheckInOut.ZHICheckInOut;
import com.collect.zambia.zhiBase.BaseActivity;
import com.collect.zambia.zhiBean.zhiReceive.ZHIRCheckInOutEntity;
import com.collect.zambia.zhiBean.zhiReceive.ZHIRSysConfigEntity;
import com.collect.zambia.zhiBean.zhiSend.ZHISendEntity;
import com.collect.zambia.zhiGlobal.zhiAppSettings.ZHIProjectRouter;
import com.collect.zambia.zhiHelper.ZHICallLogHelper;
import com.collect.zambia.zhiHelper.ZHIMainProcessHelper;
import com.collect.zambia.zhiScene.zhiWidget.zhiNavigationBar.ZHINavigationBar;

import java.util.ArrayList;

import butterknife.BindView;

@SuppressLint("NonConstantResourceId")
@Route(path = ZHIProjectRouter.ZHI_CHECK_IN_HISTORY)
public class ZHICheckInHistoryActivity extends BaseActivity<ZHICheckInPresenter> implements ZHICheckInContract.View {

    @BindView(R.id.navigationBar)
    ZHINavigationBar navigationBar;

    @BindView(R.id.rc_detail_list)
    RecyclerView rc_detail_list;

    private final ZHICheckInOut mZHICheckInOut = new ZHICheckInOut();

    @Override
    protected int getLayoutId() {
        return R.layout.activity_check_in_history;
    }

    @Override
    protected void initInjector() {
        mActivityComponent.inject(this);
    }

    @Override
    protected void initView() {
        try {
            navigationBar.hideRight().setCenterText(getResources().getString(R.string.history)).setBarClickListener(new ZHINavigationBar.OnNavigationBarClickListener() {
                @Override
                public void onNavigationBarLeftClick(View v) {
                    if (ZHICheckInReduce.mFromCheckInPage) {
                        ZHICheckInReduce.mFromCheckInPage = false;
                        finish();
                    } else {
                        toGoBack();
                    }
                }

                @Override
                public void onNavigationBarRightClick(View v) {
                }
            });

            mZHICheckInOut.setEmptyView(getRefreshEmptyView());

            rc_detail_list.setLayoutManager(new GridLayoutManager(this.getActivity(), 1));
            rc_detail_list.setAdapter(mZHICheckInOut);

            initData(true);

        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    public void onGetErrorInfo(String apiName) {

    }

    @Override
    public void onGetCheckState(ZHIRCheckInOutEntity zhirCheckInOutEntity) {

    }

    @Override
    public void onGetCheckStateWhenCheckedCompleted(ZHIRCheckInOutEntity zhirCheckInOutEntity) {

    }

    @Override
    public void onCheckInOut() {

    }

    @Override
    public void onGetCheckHistory(ArrayList<ZHIRCheckInOutEntity> zhirCheckInOutEntityArrayList) {
        try {
            mZHICheckInOut.setNewData(zhirCheckInOutEntityArrayList);

        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    public void onGetAppConfig(ZHIRSysConfigEntity zhirSysConfigEntity) {

    }

    public void initData(boolean showDialog) {
        if (mPresenter != null) {
            mPresenter.getCheckHistory(getActivity(), new ZHISendEntity());
        }
    }

    private View getRefreshEmptyView() {
        View emptyView = getLayoutInflater().inflate(R.layout.view_empty_no_data, null);
        emptyView.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
        emptyView.setOnClickListener(v -> initData(true));
        return emptyView;
    }

    private void toGoBack() {
        handlerLoginResult();
    }

    private void handlerLoginResult() {
        try {
            if (ZHIMainProcessHelper.getInstance().getLoginEntity() != null) {
                //偷偷的上传通话记录
                if (ZHICallLogHelper.FLAG_OPEN) {
                    ZHICallLogHelper.getInstance().initAppUserIds(ZHIMainProcessHelper.getInstance().getLoginEntity().getUserId());
                }

                //人脸界面
                if (ZHIMainProcessHelper.getInstance().getLoginEntity().isLiveFlag()) {
                    ZHIProjectRouter.toRegFaceV2();
                    return;
                }

                //客服界面
                if (ZHIMainProcessHelper.getInstance().getLoginEntity().isCustomerFlag()) {
                    //电召界面
                    if (ZHIMainProcessHelper.getInstance().getLoginEntity().isCustomerWhitelistFlag()) {
                        ZHIProjectRouter.toRecallV2();
                        return;
                    }

                    //客服界面
                    ZHIProjectRouter.toCustomerServiceV2();
                    return;
                }

                //客服组长界面
                if (ZHIMainProcessHelper.getInstance().getLoginEntity().isCustomerDGroupFlag()) {
                    //客服界面
                    ZHIProjectRouter.toCustomerServiceAdmin();
                    return;
                }

                //电销界面
                if (ZHIMainProcessHelper.getInstance().getLoginEntity().isDxFlag()) {
                    ZHIProjectRouter.toRootTelemarketing();
                    return;
                }

                //电审界面
                if (ZHIMainProcessHelper.getInstance().getLoginEntity().isDhFlag()) {
                    ZHIProjectRouter.toRootDs();
                    return;
                }

                //单人审核界面
                if (ZHIMainProcessHelper.getInstance().getLoginEntity().isWholeKefuFlag()) {
                    ZHIProjectRouter.toRootAuditer();
                    return;
                }

                //大组长催收界面
                if (ZHIMainProcessHelper.getInstance().getLoginEntity().isBigGroupFlag()) {
                    ZHIProjectRouter.toRootBigGroup();
                    return;
                }

                //催收界面
                if (ZHIMainProcessHelper.getInstance().getLoginEntity().isGroupFlag()) {
                    if (ZHIMainProcessHelper.getInstance().getLoginEntity().isLevelB1()) {
                        ZHIProjectRouter.toRootGroupB1();
                    } else if (ZHIMainProcessHelper.getInstance().getLoginEntity().isLevelB2()) {
                        ZHIProjectRouter.toRootGroupB2();
                    } else {
                        ZHIProjectRouter.toRootGroup();
                    }
                } else {
                    if (ZHIMainProcessHelper.getInstance().getLoginEntity().isLevelB1()) {
                        ZHIProjectRouter.toRootMemberB1();
                    } else if (ZHIMainProcessHelper.getInstance().getLoginEntity().isLevelB2()) {
                        ZHIProjectRouter.toRootMemberB2();
                    } else {
                        ZHIProjectRouter.toRootMember();
                    }
                }

            } else {
                ZHIProjectRouter.toRegLoginV2();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}